﻿package 
{
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.ui.Keyboard;
	import org.ascollada.utils.Logger;
	import org.papervision3d.events.FileLoadEvent;
	import org.papervision3d.materials.BitmapFileMaterial;
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.objects.special.DAEMC2;
	import org.papervision3d.Papervision3D;
	import org.papervision3d.view.BasicView;
	
	/**
	 * ...
	 * @author Pablo Bandin (p48l0.84nd1n@gmail.com)
	 */
	public class Main extends BasicView
	{
		[Embed(source='../bin/model.dae', mimeType="application/octet-stream")]
		private var model_DATA:Class;
		[Embed(source = '../bin/demoman_red.jpg')]
		private var model_texture_data:Class;
		
		protected var signature:String = '<a href="http://tracehello.wordpress.com/2009/04/29/controlling-dae-animations-in-papevision3d/"><u>Controlling DAE animations in Papervision3d Demo</u></a><br>by Pablo Bandin (<a href="http://tracehello.wordpress.com"><u>tracehello.wordpress.com</u></a>) Abr-2009<br>3D model by Tom Tallian (<a href="http://www.tomtallian.com"><u>www.tomtallian.com</u></a>)<br><br>'
		protected var model:DAEMC2;
		protected var debugtext:TextField;
		
		public function Main():void 
		{
			model = new DAEMC2(true, null, 0);
			
			
			
			var material:BitmapMaterial = new BitmapMaterial(Bitmap(new model_texture_data()).bitmapData, true); 
				material.doubleSided = true;
				
			model.load(XML(new model_DATA()), new MaterialsList( { demoman_red: material } ));
			model.scale = 2.3;
			model.y -= 150
			model.addEventListener(FileLoadEvent.ANIMATIONS_COMPLETE, addIt2Stage);
			
			startRendering();
			
			var tf:TextFormat = new TextFormat("Verdana", 10, 0);
			debugtext = new TextField();
			debugtext.multiline = true;
			debugtext.autoSize = TextFieldAutoSize.LEFT;
			debugtext.defaultTextFormat = tf;
			addChild(debugtext);
			
			
			debugtext.htmlText = 'Processing model animation... please wait a few seconds';
			
		}
		
		override protected function onRenderTick(event:Event = null):void 
		{
			super.onRenderTick(event);
			model.yaw(0.5);
			
			if(model.isPlaying)
			debugtext.htmlText = signature +'<br>---<br>model.currentFrame = ' + model.currentFrame + '<br>model.curentFrameNumber = ' + model.currentFrameNumber + '<br>model.isPlaying = ' + model.isPlaying + '<br>model.Xframes = ' + model.Xframes + '<br>model.inReverse = ' + model.inReverse;
			
		}	
		
		
		protected function addIt2Stage(e:FileLoadEvent):void 
		{
			scene.addChild(model);
			model.currentFrameNumber = 120;
			model.play();
			
			//model.addFrameScript(244, doLoop);
			
			signature += ("<br>Model loaded (" + model.totalFrames + " frames)<br><b>Press LEFT or RIGHT to decrease or increase the Xframe<br>Press SPACE to toggle reverseMode</b>");
			debugtext.htmlText = signature;
			
			stage.addEventListener(KeyboardEvent.KEY_DOWN, lisenKeys);
		}
		
		protected function lisenKeys(e:KeyboardEvent):void 
		{
			if (e.keyCode == Keyboard.ENTER)
			{
				model.currentFrameNumber = 120;
			}
			
			if (e.keyCode == Keyboard.LEFT)
			{
				if (model.Xframes-- < 0)
					model.Xframes = 0;
			}
			else if (e.keyCode == Keyboard.RIGHT){
				model.Xframes++;
			}
			
			if (e.keyCode == Keyboard.SPACE)
			{
				model.inReverse = !model.inReverse;
			}
		}
		
		private function doLoop():void
		{
			model.currentFrameNumber = 185;
			model.play();
		}
		
		
		
	}
	
}